package com.situ.company.project.Servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import com.situ.company.project.model.ProjectModel;
import com.situ.company.project.service.ProjectService;
import com.situ.company.util.FmtRequest;

@WebServlet("/project")
public class ProjectServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;
	
	private ProjectService service = new ProjectService();
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		String action = req.getParameter("action");
		String result="";
		switch(action) {
		case "add":
			result = add(req);
			break;
		case "upd":
			result = upd(req);
			break;
		case "list":
			result = list(req);
			break;
		case "get":
			result = get(req);
			break;
		case "del":
			result = del(req);
			break;
		case "sel":
			result = sel(req);
			break;
		default:
			break;	
		}
		FmtRequest.out(resp,result);
	}

	private String list(HttpServletRequest req) {
		String code =req.getParameter("code");
		String name =req.getParameter("name");
		String pageIndex =req.getParameter("pageIndex");
		String pageLimit =req.getParameter("pageLimit");
		ProjectModel model =new ProjectModel(code,name);
		model.setPageIndex(Integer.parseInt(pageIndex));
		model.setPageLimit(Integer.parseInt(pageLimit));
		model.setPageOn(true);
		Map<String,Object> map = new HashMap<>();
		map.put("data", service.selectList(model));
		map.put("count", service.selectCount(model));
		return new JSONObject(map).toString();
	}

	private String sel(HttpServletRequest req) {
		String code =req.getParameter("code");
		String name =req.getParameter("name");
		ProjectModel model = new ProjectModel(code, name);	
		List<ProjectModel> list =  service.selectList(model);	
		return new JSONArray(list).toString();
	}
	
	private String get(HttpServletRequest req) {
		String code = req.getParameter("code");
		ProjectModel model = new ProjectModel(code);
		ProjectModel mod = service.selectModel(model);
		return new JSONObject(mod).toString();
	}
	
	private String upd(HttpServletRequest req) {
		String code =req.getParameter("code");
		String name =req.getParameter("name");
		String time =req.getParameter("time");
		ProjectModel model = new ProjectModel(code, name, time);
		return service.update(model);
	}

	private String add(HttpServletRequest req) {
		String code =req.getParameter("code");
		String name =req.getParameter("name");
		String time =req.getParameter("time");
		System.out.println(code);
		ProjectModel model = new ProjectModel(code, name, time);
		return service.insert(model);
	}
	
	private String del(HttpServletRequest req) {
		String code = req.getParameter("code");
		ProjectModel model = new ProjectModel(code);		
		return service.delete(model);
	}
}
